package {
	import com.tudou.display.RectSprite;
	import com.tudou.top2.controller.LoadPicts;
	import com.tudou.top2.controller.ReadFromJs;
	import com.tudou.top2.events.PictEvent;
	import com.tudou.top2.model.MonitorData;
	import com.tudou.top2.view.PlacePicts;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.Security;
	import flash.system.System;
	
	[SWF(width=960, height=370, frameRate=30, backgroundColor=0x000000)]
	
	
	public class KiliTop2 extends Sprite
	{
		public static const flashWidth:uint = 960;
		
		public static const flashHeight:uint = 370;
		
		public static const adBannerWidth:uint = 224;
		
		public static const adBannerHeight:uint = 370;
		
		public static const iconWidth:uint = 100;
		
		public static const iconHeight:uint = 100;
		
		public static var betweenWidth:Number = 0;
		
//		public static const adPosition:int = 480110;
		
		//小图的宽
		public static const smallWidth:uint = 120;
		//小图的长
		public static const smallHeight:uint = 90;
		
		//取得视频大小图
		public static const getSmallBigPictFunName:String = "getFlashList";
		
		//取得广告位数据信息
		public static var getAdInfoFunName:String = "getAd";
		
		//完成从JS读取数据
		private var readJs:ReadFromJs;
		
		//完成加载图片和显示标题 
		private var loadPicts:LoadPicts;
		
		//将图片放置显示
		private var placePicts:PlacePicts;

		//向左移的Mask
		public static var myMask:Sprite;

		
		public static var monitor:MonitorData;
		
		public function KiliTop2()
		{
			Security.allowDomain('*'); 

			System.useCodePage = true;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			mask = new RectSprite(3000, 370);
			
			readJs = new ReadFromJs();
			loadPicts = new LoadPicts();
			placePicts = new PlacePicts();
			monitor = new MonitorData();
			myMask = new Sprite();
			myMask.graphics.beginFill(0xff0000, 0);
			myMask.graphics.drawRect(0, 0, 960, 370);
			addChild(myMask);
			
			
			initListener();
			
			resizeSprite();
		}
		
		private function initListener():void
		{
			loaderInfo.addEventListener(Event.INIT, initHandler);
			stage.addEventListener(Event.RESIZE, resizeHandler);
			
			readJs.addEventListener(PictEvent.READ_URL_FINISHED, readUrlFinishedHandler);
			
			loadPicts.addEventListener(PictEvent.LOAD_PICT_FINISHED, loadPictFinishedHandler);
			loadPicts.addEventListener(PictEvent.LOAD_ALL_ADPICTS_FINISHED, loadAllAdHlr);
			
			placePicts.addEventListener(PictEvent.PLACE_PICT_FINISHED, placePictFinishedHandler);
		}
		
		private function readUrlFinishedHandler(pe:PictEvent):void
		{
			trace(pe.type);
			readJs.removeEventListener(PictEvent.READ_URL_FINISHED, readUrlFinishedHandler);
			
			loadPicts.load(readJs.pictsUrl, readJs.adUrl);
		}
		//四第小图一张大图加载完毕
		private function loadPictFinishedHandler(pe:PictEvent):void
		{
			trace(pe.type);
			loadPicts.removeEventListener(PictEvent.LOAD_PICT_FINISHED, loadPictFinishedHandler);
			
			placePicts.placePicts(loadPicts.pictsData, loadPicts);
		}
		//广告图片加载完毕
		private function loadAllAdHlr(pe:PictEvent):void
		{
			trace(pe.type);
			loadPicts.removeEventListener(PictEvent.LOAD_ALL_ADPICTS_FINISHED, loadAllAdHlr);
			
			placePicts.placeAd(loadPicts.adData);
		}
		
		private function placePictFinishedHandler(pe:PictEvent):void
		{
			trace(pe.type);
			addChild(placePicts);
			placePicts.startTurn();
		}
		//从加载的参数中读取数据
		private function initHandler(e:Event):void
		{
			//发送统计时用
			monitor.userId = loaderInfo.parameters["uid"];
			monitor.juid = loaderInfo.parameters["juid"];
			//取广告位数据的JS方法名
			getAdInfoFunName = loaderInfo.parameters["fname"];
			
			if(getAdInfoFunName == null)
			{
//				throw "Can not get getData's function Name!";
				getAdInfoFunName = "flashCallAdSide";
			}
			readJs.read();
		}		
		
		private function resizeHandler(e:Event):void
		{
			resizeSprite();
		}
		
		private function resizeSprite():void
		{
			KiliTop2.betweenWidth = (Math.max(stage.stageWidth, flashWidth) - flashWidth) / 2;
			placePicts.x = KiliTop2.betweenWidth;
			myMask.x = placePicts.x;
			myMask.y = placePicts.y;
			//改变其它图片的位置
			placePicts.resetBigPictPosition();
		}
		
	}
}





